﻿package net.wangit.util;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

@SuppressWarnings("unchecked")
public class MapUtil {

	public MapUtil() {
	}

	public static final String getString(Map map, String key) {
		return String.valueOf(map.get(key));
	}

	public static final String getString(Map map, String key,
			String defaultValue) {
		Object value = map.get(key);
		return value != null ? value.toString() : defaultValue;
	}

	public static final int getInt(Map map, String key, int defaultValue) {
		String integer = getString(map, key);
		return NumberUtil.isInt(integer) ? Integer.parseInt(integer)
				: defaultValue;
	}

	public static void print(Map m) {
		if (m != null) {
			System.out.println("Map printer:");
			Set keySet = m.keySet();
			Object key;
			Object value;
			for (Iterator keys = keySet.iterator(); keys.hasNext(); System.out
					.println((new StringBuilder("key=")).append(key).append(
							", value=").append(value).toString())) {
				key = keys.next();
				value = m.get(key);
			}

		} else {
			System.out.println("Map printer: null");
		}
	}
}
